package login.Model.dao;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.util.Random;

/**
 * 生成验证码
 */

public class VerifiCode {
        private int width = 100;  //验证码的宽
        private int height = 40;    //验证码的高
        private String getCodes;  //保存验证码的文本内容
        private Random r = new Random();    //获取随机数对象
        //字体数组
        private String[] Font = {"Georgia"};
        //验证码数组
        private String codes = "0123456789abcdefghjkmnopqrstuvwxyzABCDEFGHJKMNPQRSTUVWXYZ";

        /**
         * 获取随机的颜色
         * @return
         */
        private Color randomColor() {
            int r = this.r.nextInt(225);
            int g = this.r.nextInt(225);
            int b = this.r.nextInt(225);
            return new Color(r, g, b);            //返回一个随机颜色
        }

        /**
         * 获取随机字体
         * @return
         */
        private Font randomFont() {
            int index = r.nextInt(Font.length);
            String font = Font[index];
            int style = r.nextInt(4);         //字体的样式
            int size = r.nextInt(10) + 24;    //字体的大小
            return new Font(font, style, size);   //返回一个随机的字体
        }

        /**
         * 获取随机字符
         * @return
         */
        private char randomChar() {
            int index = r.nextInt(codes.length());
            return codes.charAt(index);
        }

        /**
         * 画干扰线，验证码干扰线用来防止计算机解析图片
         * @param
         */
        private void drawLine(BufferedImage image) {
            int num = r.nextInt(10); //默认干扰线的数量
            Graphics2D g = (Graphics2D) image.getGraphics();
            for (int i = 0; i < num; i++) {
                int x1 = r.nextInt(width);
                int y1 = r.nextInt(height);
                int x2 = r.nextInt(width);
                int y2 = r.nextInt(height);
                g.setColor(randomColor());
                g.drawLine(x1, y1, x2, y2);
            }
        }

        /**
         * 创建验证码图片的方法
         * @return
         */
        private BufferedImage createImage() {
            //创建图片缓冲区
            BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
            //获取画笔
            Graphics2D g = (Graphics2D) image.getGraphics();
            //设置背景色
            g.setColor(new Color(255, 255, r.nextInt(245) + 10));
            g.fillRect(0, 0, width, height);
            //返回一个图片
            return image;
        }

        /**
         * 获取验证码的方法
         * @return
         */
        public BufferedImage getImage() {
            BufferedImage image = createImage();
            Graphics2D g = (Graphics2D) image.getGraphics(); //获取画笔
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < 4; i++)             //生成随机字符
            {
                String s = randomChar() + "";      //字符变成字符串
                sb.append(s);                      //添加到StringBuilder里面
                float x = i * 1.0F * width / 4;   //定义字符的x坐标
                g.setFont(randomFont());           //设置字体
                g.setColor(randomColor());         //设置颜色
                g.drawString(s, x, height - 5);
            }
            this.getCodes = sb.toString();
            drawLine(image);
            return image;
        }

        /**
         * 获取验证码文本的方法
         * @return
         */
        public String getCodes() {
            return getCodes;
        }

        /**
         * 刷新验证码
         */
        public void update(){
            createImage();
        }


}
